package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcUserService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.core.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 业务用户
 * 
 * @ClassName: CdcUserController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:06
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcuser")
public class CdcUserController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcuser/";

	@Autowired
	private ICdcUserService cdcUserService;
    @Autowired
    private IUserService userService;
	

    /**
     * 跳转到业务用户列表首页
     */
    @RequestMapping("")
    @Permission("cdcuser:list")
    public String index() {
        return PREFIX + "cdcuser.html";
    }
	
    /**
     * 跳转到新增业务用户
     */
    @RequestMapping("/to_add")
    @Permission("cdcuser:add")
    public String toAdd() {
        return PREFIX + "cdcuser_add.html";
    }
	
	/**
     * 跳转到修改业务用户
     */
    @RequestMapping("/to_update/{cdcUserId}")
    @Permission("cdcuser:update")
    public String toUpdate(@PathVariable Long cdcUserId, Model model) {
        CdcUserEntity cdcUser = this.cdcUserService.selectById(cdcUserId);
        if (CommonUtil.isNotEmpty(cdcUser.getSysUserId())) {
            User sysUser = this.userService.selectById(cdcUser.getSysUserId());
            cdcUser.setSysUser(sysUser);
        }
        model.addAttribute("cdcUser",cdcUser);
        return PREFIX + "cdcuser_edit.html";
    }

    /**
     * 获取业务用户列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcuser:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcUserEntity> page = new PageFactory<CdcUserEntity>().defaultPage();
        List<CdcUserEntity> result = this.cdcUserService.page(page,params);
        page.setRecords((List<CdcUserEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增业务用户
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcuser:add")
    @BussinessLog(value = "新增业务用户")
    public Object add(CdcUserEntity cdcUser) {
        if (CommonUtil.isOneEmpty(cdcUser)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcUser.insert();
        return R.ok();
    }

    /**
     * 删除业务用户
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcuser:delete")
    @BussinessLog(value = "删除业务用户")
    public Object delete(@RequestParam Long cdcUserId) {
	
        this.cdcUserService.deleteById(cdcUserId);

        return R.ok();
    }

    /**
     * 修改业务用户
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcuser:update")
    @BussinessLog(value = "修改业务用户")
    public Object update(CdcUserEntity cdcUser) {
        if (CommonUtil.isOneEmpty(cdcUser)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcUserEntity old = this.cdcUserService.selectById(cdcUser.getId());

        old.setXcode(cdcUser.getXcode());
        old.setCode(cdcUser.getCode());
        old.setName(cdcUser.getName());
        old.setBusUserTypeId(cdcUser.getBusUserTypeId());
        old.setBusUserLevelId(cdcUser.getBusUserLevelId());
        old.setDataId(cdcUser.getDataId());
        old.setSysUserId(cdcUser.getSysUserId());
        old.setIsActive(cdcUser.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
